/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.server_tunnel.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import sshtunneler.library.DefaultPorts;
import sshtunneler.library.Server;
import sshtunneler.library.Tunnel;
import sshtunneler.server_tunnel.tree.GenericServerTunnelTree;

public class AddTunnel extends javax.swing.JDialog {
	private static final long serialVersionUID = -9046856586689583804L;
	private Server server;
	private Tunnel tunnel;
	private GenericServerTunnelTree tree;

	public AddTunnel(java.awt.Frame parent, Server server, Tunnel tunnel,
			GenericServerTunnelTree tree) {
		super(parent);
		this.tree = tree;
		this.server = server;
		this.tunnel = tunnel;
		this.initComponents();

		this.rootPane.setDefaultButton(this.cmd_add);

		Action close = new AbstractAction() {
			private static final long serialVersionUID = 6743289239934467164L;

			public void actionPerformed(ActionEvent e) {
				AddTunnel.this.cmd_cancelActionPerformed(null);
			}
		};
		this.getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke(java.awt.Event.ESCAPE, 0), "close");
		this.getRootPane().getActionMap().put("close", close);

		for (String p : DefaultPorts.inst.get_all_port_names()) {
			this.cmb_protocol.addItem(p);
		}
		this.cmb_protocol.addItem("Other");
		if (this.tunnel != null) {
			this.txt_local_port.setText("" + tunnel.getLocalPort());
			this.txt_remote_port.setText("" + tunnel.getRemotePort());
			this.txt_remote_server.setText("" + tunnel.getRemoteServer());
			this.cmb_protocol.setSelectedItem(DefaultPorts.inst
					.find_service(tunnel.getRemotePort()));
			this.cmd_add.setText("Modify");
		}
	}

	private void initComponents() {

		this.jPanel1 = new javax.swing.JPanel();
		this.jLabel1 = new javax.swing.JLabel();
		this.txt_local_port = new javax.swing.JTextField();
		this.jPanel2 = new javax.swing.JPanel();
		this.jLabel2 = new javax.swing.JLabel();
		this.txt_remote_server = new javax.swing.JTextField();
		this.jPanel3 = new javax.swing.JPanel();
		this.jLabel3 = new javax.swing.JLabel();
		this.cmb_protocol = new javax.swing.JComboBox();
		this.txt_remote_port = new javax.swing.JTextField();
		this.jPanel4 = new javax.swing.JPanel();
		this.cmd_cancel = new javax.swing.JButton();
		this.cmd_add = new javax.swing.JButton();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Add tunnel");
		this.setModal(true);
		this.setName("Form");
		this.setResizable(false);
		this.getContentPane().setLayout(
				new javax.swing.BoxLayout(this.getContentPane(),
						javax.swing.BoxLayout.Y_AXIS));

		this.jPanel1.setName("jPanel1");

		this.jLabel1.setText("Local port:");
		this.jLabel1.setName("jLabel1");
		this.jPanel1.add(this.jLabel1);

		this.txt_local_port.setMaximumSize(new java.awt.Dimension(300, 20));
		this.txt_local_port.setMinimumSize(new java.awt.Dimension(150, 20));
		this.txt_local_port.setName("txt_local_port");
		this.txt_local_port.setPreferredSize(new java.awt.Dimension(150, 20));
		this.jPanel1.add(this.txt_local_port);

		this.getContentPane().add(this.jPanel1);

		this.jPanel2.setName("jPanel2");

		this.jLabel2.setText("Remote server:");
		this.jLabel2.setName("jLabel2");
		this.jPanel2.add(this.jLabel2);

		this.txt_remote_server.setMaximumSize(new java.awt.Dimension(300, 20));
		this.txt_remote_server.setMinimumSize(new java.awt.Dimension(150, 20));
		this.txt_remote_server.setName("txt_remote_server");
		this.txt_remote_server
				.setPreferredSize(new java.awt.Dimension(150, 20));
		this.jPanel2.add(this.txt_remote_server);

		this.getContentPane().add(this.jPanel2);

		this.jPanel3.setName("jPanel3");

		this.jLabel3.setText("Remote port:");
		this.jLabel3.setName("jLabel3");
		this.jPanel3.add(this.jLabel3);

		this.cmb_protocol.setMaximumSize(new java.awt.Dimension(300, 18));
		this.cmb_protocol.setMinimumSize(new java.awt.Dimension(100, 18));
		this.cmb_protocol.setName("cmb_protocol");
		this.cmb_protocol.setPreferredSize(new java.awt.Dimension(100, 20));
		this.cmb_protocol
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						AddTunnel.this.cmb_protocolActionPerformed(evt);
					}
				});
		this.jPanel3.add(this.cmb_protocol);

		this.txt_remote_port
				.setToolTipText("<html>Common ports:<br>-Remote Desktop: 3389</html>");
		this.txt_remote_port.setMaximumSize(new java.awt.Dimension(100, 20));
		this.txt_remote_port.setMinimumSize(new java.awt.Dimension(50, 20));
		this.txt_remote_port.setName("txt_remote_port");
		this.txt_remote_port.setPreferredSize(new java.awt.Dimension(50, 20));
		this.jPanel3.add(this.txt_remote_port);

		this.getContentPane().add(this.jPanel3);

		this.jPanel4.setName("jPanel4");

		this.cmd_cancel.setText("Cancel");
		this.cmd_cancel.setName("cmd_cancel");
		this.cmd_cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddTunnel.this.cmd_cancelActionPerformed(evt);
			}
		});
		this.jPanel4.add(this.cmd_cancel);

		this.cmd_add.setText("Add");
		this.cmd_add.setName("cmd_add");
		this.cmd_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddTunnel.this.cmd_addActionPerformed(evt);
			}
		});
		this.jPanel4.add(this.cmd_add);

		this.getContentPane().add(this.jPanel4);

		this.pack();
	}

	private void cmd_cancelActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void cmd_addActionPerformed(java.awt.event.ActionEvent evt) {
		String str_local_port = this.txt_local_port.getText();
		String str_remote_port = this.txt_remote_port.getText();
		String remote_server = this.txt_remote_server.getText();
		int local_port;
		int remote_port;

		this.txt_local_port.setBorder(BorderFactory
				.createLineBorder(Color.BLACK));
		this.txt_remote_port.setBorder(BorderFactory
				.createLineBorder(Color.BLACK));
		this.txt_remote_server.setBorder(BorderFactory
				.createLineBorder(Color.BLACK));

		if (str_local_port == null || str_local_port.equals("")) {
			this.txt_local_port.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this, "All fields are mandatory.",
					"Missing information", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (str_remote_port == null || str_remote_port.equals("")) {
			this.txt_remote_port.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this, "All fields are mandatory.",
					"Missing information", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (remote_server == null || remote_server.equals("")) {
			this.txt_remote_server.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this, "All fields are mandatory.",
					"Missing information", JOptionPane.ERROR_MESSAGE);
			return;
		}
		try {
			local_port = Integer.parseInt(str_local_port);
		} catch (NumberFormatException e) {
			this.txt_local_port.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this,
					"The port should be a number !", "Not a number",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		try {
			remote_port = Integer.parseInt(str_remote_port);
		} catch (NumberFormatException e) {
			this.txt_remote_port.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this,
					"The port should be a number !", "Not a number",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (this.tunnel != null) {
			this.tunnel.setLocalPort(local_port);
			this.tunnel.setRemotePort(remote_port);
			this.tunnel.setRemoteServer(remote_server);
			this.tree.process_tunnel_action(false, false, true);
		} else {
			this.server.addTunnel(remote_server, remote_port, local_port);
			this.tree.process_tunnel_action(true, false, false);
		}
		this.dispose();
	}

	private void cmb_protocolActionPerformed(java.awt.event.ActionEvent evt) {
		int port = DefaultPorts.inst.find_port(this.cmb_protocol
				.getSelectedItem().toString());

		this.txt_remote_port.setEnabled(false);
		if (port == 0) {
			this.txt_remote_port.setEnabled(true);
		}
		this.txt_remote_port.setText("" + port);
	}

	private javax.swing.JComboBox cmb_protocol;
	private javax.swing.JButton cmd_add;
	private javax.swing.JButton cmd_cancel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JTextField txt_local_port;
	private javax.swing.JTextField txt_remote_port;
	private javax.swing.JTextField txt_remote_server;

}
